﻿using System.Xml;
using System.Xml.Serialization;
using Barbar.Colonization.Core.Configuration;

namespace Barbar.Colonization.Core.Localization.XmlProvider {
  internal class XmlTranslatorFactory : ITranslatorFactory {
    public const string NamespaceDescription = "http://codeplex.com/colonization/xmllanguage.xsd";

    #region ITranslatorFactory Members

    public void FillTranslator(Translator translator) {
      XmlSerializer serializer = new XmlSerializer(typeof(Languages), NamespaceDescription);
      Languages languages = null;
      using (XmlReader reader = XmlReader.Create(ColonizationConfig.Instance.LanguagePath)) {
        languages = (Languages)serializer.Deserialize(reader);
      }

      foreach (Key key in languages.Keys) {
        foreach (Item item in key.Items) {
          TranslatorKey translatorKey = languages.GetTranslatorKey(key.Code, item);
          translator.Add(translatorKey, item.Value);
        }
      }
    }

    #endregion
  }
}
